package com.guiwu.gteway.industryapp.Device.module;

import com.guiwu.gteway.industryapp.Frame.FrameHelper;

/**
 * Created by qiaoguan on 2018/3/24. plc类型的监控点
 */

public class PLCMonitor extends Monitor {
    private static final String S = "S";
    private static final String M = "M";
    private static final String X = "X";
    private static final String Y = "Y";
    private static final String T = "T";
    private static final String D = "D";
    private static final String C = "C";



    public PLCMonitor(MonitorData monitorData) {
        super(monitorData);
    }

    public int getMonitorType(){
        if (dataType.equalsIgnoreCase("bit")){
            return BIT;
        }else if (dataType.equalsIgnoreCase("bit16"))
            return BIT16;
        else return BIT32;
    }

    @Override
    public String getValue() {
        return addDeciaml(value,decimal[1]) + " " + unit;
    }

    @Override
    public String getTypeValue(int inputValue) {
        return  addDeciaml(inputValue,decimal[1]) + " " + unit;
    }

    @Override
    public void sendCmd(String deviceId, String apiKey, boolean coil) {
        String cmdFrame;
        if ((cmdFrame = getCoilFrame(coil)) == null)
            return;
        handleCmd(deviceId,apiKey,cmdFrame);
    }

    @Override
    public void sendCmd(String deviceId, String apiKey, int value) {
        String cmdFrame;
        if ((cmdFrame = getRegFrame(value)) == null)
            return;
        handleCmd(deviceId,apiKey,cmdFrame);
    }

    @Override
    public String getCoilFrame(boolean coil){
        int address = 0;
        String cmd = null;
        if (coil == getSwitchState())
            return null;
        //判断寄存器
        switch (addressType.toUpperCase()) {
            case S:
                address = 0 + addressAdd;
                break;
            case M:
                if (addressAdd < 1536){
                    address = 2048 + addressAdd;
                }else {
                    address = 45056 + addressAdd;
                }
                break;
            case X:
                address = 1024 + addressAdd;
                break;
            case Y:
                address = 1280 + addressAdd;
                break;
            case T:
                address = 1536 + addressAdd;
                break;
        }
        //判断协议
        if (protocol.equalsIgnoreCase("RTU")) {
            cmd = FrameHelper.writeCoilRTU(slaveAdr,address,coil);
        }else if (protocol.equalsIgnoreCase("ASCII")){
            cmd = FrameHelper.writeCoilASSIC(slaveAdr,address,coil);
        }
        return cmd;
    }

    @Override
    public String getRegFrame(int value) {
        int address = 0;
        String cmd = null;
         //寄存器
        switch (addressType.toUpperCase()){
            case D:
                address = 4096 + addressAdd;
                break;
            case T:
                address = 1536 + addressAdd;
                break;
            case C:
                if (getMonitorType() == BIT32)
                    address = 3784 + addressAdd;
                else if (getMonitorType() == BIT16)
                    address = 3584 + addressAdd;
                break;
        }
        //协议类型
        if (protocol.equalsIgnoreCase("RTU")){
            cmd = FrameHelper.wrightRegisterRTU(slaveAdr,address,value);
        }else if (protocol.equalsIgnoreCase("ASCII")){
            cmd = FrameHelper.writeRegistASSIC(slaveAdr,address,value);
        }

        return cmd;
    }


    @Override
    public String toString() {
        return "类型：plc    " + super.toString();
    }
}
